

#pragma once

#include "type.h"

#include "towerinfo.h"
#include "gamemessage.h"

#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

#define LM					LevelManager::getInstance()

class Enemy;
class GamePlayScene;

struct SceneEntityData {
	int x;
	int y;
	string type;
	string resourceName;
};

class LevelManager : public MessageHolder {
public:
	static LevelManager* getInstance();
	~LevelManager(){};

	void init();
	void loadLevel(const char* fileName);
	void startNextWave();

	bool isPointInRoadArea(const Point& pt);
	vector<int> getWaysOfWave(int waveIndex);

	void update(float ticks);
	void handleMessage();

	Point getCastlePosition();

	int getWaveWaitingPercent() {
		int ret = 0;

		if(enemyWaveIndex < (int)enemyWaves.size()) {
			static_cast<int>(_waveStartDelayTicks / enemyWaves[enemyWaveIndex].delay * 100);
			if(ret > 100) {
				ret = 100;
			}
		}

		return ret;
	}

public:
	vector<SceneEntityData> sceneEntities;
	vector<WayInfo> ways;
	vector<WaveInfo> enemyWaves;
	vector<TowerInfo> towers;
	vector<TowerInfo> mines;
	vector<UnitInfo> heroes;
	vector<Rect> bboxes;

	string sceneBackgroundName;

	int initialGoldQuantity;

	int enemyWaveIndex;
	int enemyIndex;

	bool firstWaveStarted;
	bool hasMoreWave;

	GamePlayScene* gamePlayScene;

private:
	static LevelManager* _instance;
	LevelManager();

	void spawnEnemy(float ticks);
	Enemy* createEnemy(const EnemyInfo& info);

private:
	// the moment when the previous enemy be created
	float _previousEnemyTicks;
	// the time that should wait before start this wave
	float _waveStartDelayTicks;
	// the time elapsed
	float _ticks;

};

